package com.example.rabbitdemo.config.exchange;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 周宇
 * @version 1.0 date: 2019/7/23 10:35
 * @description: 发送者指定交换机、routekey，并绑定routekey到指定的queue；消费者直接指定queue即可消费
 * 实现Routing路由模式:生产者发送消息时需要指定routing key，交换机会根据routing key将消息投递到指定的队列
 */
@Configuration
public class DirectExchangeConfig {
  //声明一个Direct类型的交换机
  @Bean
  public DirectExchange directExchange(){
    return new DirectExchange("Routing_Direct");
  }

  //声明两个队列
  @Bean
  public Queue queue8(){
    return new Queue("queue8");
  }

  @Bean
  public Queue queue9(){
    return new Queue("queue9");
  }

  @Bean
  public Queue userQueue() {
    Map<String, Object> augus = new HashMap<>();
    return QueueBuilder.durable("QUEUE_ZHOUYU").withArguments(augus).build();
  }

  //将队列queue8绑定到交换器上，并指定binding key为orange
  @Bean
  public Binding bindingQueue8(){
    return BindingBuilder.bind(queue8()).to(directExchange()).with("orange");
  }

  //将队列queue9绑定到交换器上，并指定binding key为green
  @Bean
  public Binding bindingQueue9(){
    return BindingBuilder.bind(queue9()).to(directExchange()).with("green");
  }


  /**
   * 將队列-交换机-路由绑定
   */
  @Bean
  public Binding buildUserQueue() {
    //new BindingFactoryBean().setDestinationExchange(userExchange());
    return BindingBuilder.bind(userQueue()).to(directExchange()).with("QUEUE_ZHOUYU");
  }

}
